import * as Cesium from "cesium"
function WallGradientsMaterialProperty(color) {
  /* 跟踪材质修改 */
  this._definitionChanged = new Cesium.Event()
  this._color = undefined
  this._colorSubscription = undefined
  this.color = color
}
/* 定义WallGradientsMaterialProperty原型对象的属性 */
Object.defineProperties(WallGradientsMaterialProperty.prototype, {
  /* 指示材质属性是否是常量 */
  isConstant: {
    get: function () {
      return false
    },
  },
  /* 定义材质属性的变化事件,监听材质定义的变化 */
  definitionChanged: {
    get: function () {
      return this._definitionChanged
    },
  },
  color: Cesium.createPropertyDescriptor("color"),
})
/* 返回材质类型的字符串标识 */
WallGradientsMaterialProperty.prototype.getType = function (time) {
  return "WallGradients"
}
/* 根据给定的时间获取当前的材质属性值 */
WallGradientsMaterialProperty.prototype.getValue = function (time, result) {
  if (!Cesium.defined(result)) {
    result = {}
  }
  result.color = Cesium.Property.getValueOrClonedDefault(
    this._color,
    time,
    Cesium.Color.WHITE,
    result.color
  )
  result.image = Cesium.Material.WallGradientsImage
  return result
}
/* 判断两个材质属性是否相等 */
WallGradientsMaterialProperty.prototype.equals = function (other) {
  return (
    this === other ||
    (other instanceof WallGradientsMaterialProperty &&
      Cesium.Property.equals(this._color, other._color))
  )
}
/* 材质类型定义 */
Cesium.Material.WallGradientsType = "WallGradients"
/* 材质图像资源路径 */
Cesium.Material.WallGradientsImage =
  "./src/assets/materialResources/wallgradients.png"
Cesium.Material.WallGradientsSource =/*glsl*/`
 czm_material czm_getMaterial(czm_materialInput materialInput){
     czm_material material = czm_getDefaultMaterial(materialInput);
     vec2 st = materialInput.st;
     vec4 colorImage = texture2D(image, vec2(fract(st.t - time), st.t));
     float time=fract(czm_frameNumber/10.0);
     float isAlpha=step(0.5,time);\n\
     float dis = distance(st, vec2(0.5));
     if(isAlpha>=1.0){
        material.alpha = colorImage.a * color.a* dis *2.5;
    }else{
        material.alpha = colorImage.a * color.a * dis *2.0;
    }
     material.diffuse =  2.5 * color.rgb  ;\n\
     return material;
 }`
Cesium.Material._materialCache.addMaterial(Cesium.Material.WallGradientsType, {
  fabric: {
    type: Cesium.Material.WallGradientsType,
    uniforms: {
      color: new Cesium.Color(1.0, 0.0, 0.0, 0.4),
      image: Cesium.Material.WallGradientsImage,
      time: 0,
    },
    source: Cesium.Material.WallGradientsSource,
  },
  /* 材质是否支持透明混合 */
  translucent: function (material) {
    return true
  },
})

export default WallGradientsMaterialProperty
